package top.huhuiyu.springboot2.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.huhuiyu.springboot2.entity.*;
import top.huhuiyu.springboot2.service.TbNewsService;

import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController {
  private final Logger logger = LoggerFactory.getLogger(NewsController.class);

  // 完成部门表的增删改查，附加作业：完成班级表的增删改查

  @Autowired
  private TbNewsService tbNewsService;

  @DeleteMapping("/{id}")
  public BaseResult delete(@PathVariable("id") Integer id) {
    return tbNewsService.delete(id);
  }

  @PutMapping("")
  public BaseResult update(@RequestBody TbNews tbNews) {
    return tbNewsService.update(tbNews);
  }

  // get /news 表示查询全部的信息
  // get /news/1 表示查询id为1的信息
  @GetMapping("/{id}")
  public BaseDataResult<TbNews> queryByKey(@PathVariable("id") Integer id) {
    System.out.println("从地址栏中获取到的id值：" + id);
    return tbNewsService.queryByKey(id);
  }

  // @GetMapping只能通过get方式调用
  @GetMapping("")
  public BaseDataResult<PageResult<List<TbNews>>> query(NewsQuery query, PageBean page) {
    logger.debug("请求的参数：{},{}", query, page);
    return tbNewsService.query(query, page);
  }

  // @RequestBody表示前端使用json格式传递数据
  @PostMapping("")
  public BaseResult add(@RequestBody TbNews tbNews) {
    return tbNewsService.add(tbNews);
  }
}
